package com.googlecode.tidej.client.editor;

import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.googlecode.tidej.client.TidejController;
import com.googlecode.tidej.client.codemirror.CodeMirror;
import com.googlecode.tidej.client.project.ProjectManager;

public class HtmlEditor extends Composite {
  
  FlowPanel editorPanel = new FlowPanel();
  CodeMirror codeMirror;
  ProjectManager projectManager;
 
  static int cnt = 0;
  int nr = ++cnt;
  
  public HtmlEditor(TidejController controller) {
    Style style = editorPanel.getElement().getStyle();
    style.setBorderWidth(0, Unit.PX);
    style.setProperty("boxSizing", "border-box");
    style.setProperty("WebkitBoxSizing", "border-box");
    style.setProperty("MozBoxSizing", "border-box");

    initWidget(editorPanel); 
  }


  public void setHtml(ProjectManager projectManager) {
    this.projectManager = projectManager;
    editorPanel.getElement().setInnerText("");
    
    CodeMirror.Configuration config = CodeMirror.Configuration.create();
    config.setMode("xml");
    config.setLineNumbers(true);
    codeMirror = CodeMirror.create(editorPanel.getElement(), config);
    codeMirror.setValue(projectManager.getHtmlOnly());
    codeMirror.refresh();
  }

  
  public boolean checkModifications() {
    boolean result = false;
    if (codeMirror == null) {
      return false;
    }
    String text = codeMirror.getValue();
    if (projectManager != null && !text.equals(projectManager.getHtmlOnly())) {
      projectManager.setHtmlOnly(text);
      result = true;
    }
    return result;
  }
  
}
